import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class HospitalQueueSystem {
    public static void main(String[] args) {
        Queue<Integer> patientQueue = new LinkedList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            printMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除输入缓冲
            switch (choice) {
                case 1:
                    handleRegistration(patientQueue, scanner);
                    break;
                case 2:
                    handleConsultation(patientQueue);
                    break;
                case 3:
                    displayQueue(patientQueue);
                    break;
                case 4:
                    handleRemainingPatients(patientQueue);
                    return;
                case 5:
                    System.out.println("下班时间到，未就诊的病人请明天再来！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }
    private static void printMenu() {
        System.out.println("\n医院排队系统");
        System.out.println("1. 排队");
        System.out.println("2. 就诊");
        System.out.println("3. 查看排队");
        System.out.println("4. 不再排队，余下顺序就诊");
        System.out.println("5. 下班");
        System.out.print("请选择操作：");
    }

    private static void handleRegistration(Queue<Integer> queue, Scanner scanner) {
        System.out.print("请输入病人病历号：");
        int number = scanner.nextInt();
        scanner.nextLine();
        queue.add(number);
        System.out.println("病人 " + number + " 已加入排队队列");
    }

    private static void handleConsultation(Queue<Integer> queue) {
        if (queue.isEmpty()) {
            System.out.println("当前没有等待就诊的病人");
        } else {
            int patient = queue.poll();
            System.out.println("请病人 " + patient + " 到诊室就诊");
        }
    }

    private static void displayQueue(Queue<Integer> queue) {
        if (queue.isEmpty()) {
            System.out.println("当前没有排队病人");
            return;
        }

        System.out.println("当前排队序列：");
        int position = 1;
        for (int patient : queue) {
            System.out.println(position++ + ". " + patient);
        }
    }

    private static void handleRemainingPatients(Queue<Integer> queue) {
        System.out.println("\n未完成就诊的病人：");
        displayQueue(queue);
        System.out.println("程序已退出，剩余病人将按顺序就诊");
    }
}